import { app, BrowserWindow, dialog, ipcMain } from "electron";
import path, { join } from "path";

import { closeDevTools, openDevTools } from "../utils/devtools";
import { getIcon } from "../utils/icon";
import { ipcMainOn } from "../utils/ipcMain";

const createOtherWin = () => {
	const win = new BrowserWindow({
		width: 1200,
		height: 700,
		show: false,
		frame: true, // 让桌面应用没有边框，这样菜单栏也会消失
		icon: getIcon(),
		useContentSize: false,
        maximizable: true,
        resizable: true, // 不允许用户改变窗口大小,
        // titleBarStyle: "hidden", //开启titleBarStyle ，titleBarOverlay才能被开启，该地方又bug未完善，桌面关闭后桌面右下角打不开
        // titleBarOverlay: {
        //     color: "#f8f8f8",
        //     symbolColor: "black"
        // },
		webPreferences: {
			nodeIntegration: true,
			contextIsolation: true,
			preload: join(__dirname, "../preload/index.js")
		}
	});
    win.setMenuBarVisibility(false)
	win.webContents.on("did-finish-load", () => {
		// 利用localStorage存储win.id
		win.webContents.executeJavaScript(`localStorage.setItem("electronOtherWinId",${win.id})`);
	});

	let windowUrl;
	if (app.isPackaged) {
		windowUrl = "file://" + join(__dirname, "../../index.html");
	} else {
		windowUrl = `http://${process.env["VITE_DEV_SERVER_HOST"]}:${process.env["VITE_DEV_SERVER_PORT"]}`;
	}
	win.loadURL(windowUrl + "#/other");
	win.on('close', (e) => {
        e.preventDefault();
		win.hide();
	});


	// win.on("show", () => openDevTools(win)); //调试工具
	// win.on("hide", () => closeDevTools(win)); //关闭调试
	ipcMainOn("openOtherWin", () => {
		win.show();
		// win.hide()
	});

	return win;
};

export default createOtherWin;
